package com.dfbz.config;


import com.dfbz.interceptor.LoginInteceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.dfbz.controller")
public class SpringMvcConfig implements WebMvcConfigurer {


    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();//开启静态资源放行
    }

    //声明默认视图解析器
    @Bean
    public InternalResourceViewResolver getViewResolver(){
        return new InternalResourceViewResolver("/WEB-INF/",".html");
    }

    /**
     *
     * @return
     */
    //必須指定该bean 值为 multipartResolver，原因是mvc视图解析指定 此命名空间
    @Bean("multipartResolver")
    public CommonsMultipartResolver getCommonsMultipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        return commonsMultipartResolver;
    }


    /**
     * 配置mvc拦截类
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        LoginInteceptor inteceptor = new LoginInteceptor();
        InterceptorRegistration registration = registry.addInterceptor(inteceptor);
        registration.addPathPatterns("/**").excludePathPatterns(new String[]
                        {"/index",
                                "/user/doLogin",
                                "/user/logout",});
    }
}
